package com.bank;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.text.TextUtils;
import android.util.Log;

import com.bank.GOME_M27A.M27CIBbank;
import com.bank.lenovoz5.Z5CIBbank;
import com.entity.PointBean;
import com.entity.SMSCode;
import com.receive.SMSReceiverSend;
import com.show.api.util.StringUtils;
import com.utils.ChatClient;
import com.utils.CodeUtils;
import com.utils.Constants;
import com.utils.FileHelper;
import com.utils.ImageView;
import com.utils.LogToFile;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.os.SystemClock.sleep;
import static com.utils.ChatClient.C_updateBalance_W;
import static com.utils.ChatClient.ConfirmOrder;
import static com.utils.ChatClient.PAUSE;
import static com.utils.ChatClient.RUNING;
import static com.utils.ChatClient.S_paused_C;
import static com.utils.ChatClient.addWorkTime;
import static com.utils.ChatClient.getSerialNumber;
import static com.utils.ChatClient.getmap;
import static com.utils.ChatClient.list;
import static com.utils.ChatClient.mapDelete;
import static com.utils.ChatClient.requestSMSCode;
import static com.utils.ChatClient.setDevicesLog;
import static com.utils.ChatClient.user;
import static com.utils.Constants.orderBankName;
import static com.utils.ImageView.saveImg22;

/**
 * 首次登录手动转账一笔，在用脚本跑
 * 兴业银行自动化脚本
 * 修改时间：2019年7月25日16:29:06
 */
@RunWith(AndroidJUnit4.class)
public class CIBbank {
    private String bankName = "CIB";
    private static UiDevice mDevice;
    private static String sumMoney = "-1";
    private Context context = InstrumentationRegistry.getTargetContext();
    private static int screenHeight = 0; // 手机屏幕高度
    private boolean isChangeBill = false;
    //金额键盘
    private static int[][] xy_123m = new int[][]{
            {145, 1430}, {400, 1430}, {660, 1430},
            {145, 1600}, {400, 1600}, {660, 1600},
            {145, 1750}, {400, 1750}, {660, 1750},
            {145, 1915}, {500, 1915}};
    private static int[][] xy_123m_new = new int[][]{
            {145, 1530}, {400, 1530}, {660, 1530},
            {145, 1700}, {400, 1700}, {660, 1700},
            {145, 1850}, {400, 1850}, {660, 1850},
            {145, 2015}, {500, 2015}};

    private static int[][] xy_Card_Number;

    //卡号键盘
    private static int[][] xy_Card_Number_2016 = new int[][]{
            {300, 1410}, {540, 1410}, {780, 1410},
            {300, 1590}, {540, 1590}, {780, 1590},
            {300, 1750}, {540, 1750}, {780, 1750},
            {540, 1930}};
    //卡号键盘
    private static int[][] xy_Card_Number_2016_new = new int[][]{
            {300, 1510}, {540, 1510}, {780, 1510},
            {300, 1690}, {540, 1690}, {780, 1690},
            {300, 1850}, {540, 1850}, {780, 1850},
            {540, 2030}};

    //字母键盘
    private static int[][] xy_abc_2016 = new int[][]{
            {58, 1450}, {171, 1450}, {270, 1450}, {382, 1450}, {487, 1450}, {606, 1450}, {715, 1450}, {818, 1450}, {918, 1450}, {1017, 1450},
            {122, 1620}, {222, 1620}, {330, 1620}, {433, 1620}, {557, 1620}, {655, 1620}, {773, 1620}, {863, 1620}, {970, 1620},
            {222, 1770}, {334, 1770}, {442, 1770}, {544, 1770}, {654, 1770}, {754, 1770}, {860, 1770}};
    //字母键盘
    private static int[][] xy_abc_2016_new = new int[][]{
            {58, 1550}, {171, 1550}, {270, 1550}, {382, 1550}, {487, 1550}, {606, 1550}, {715, 1550}, {818, 1550}, {918, 1550}, {1017, 1550},
            {122, 1720}, {222, 1720}, {330, 1720}, {433, 1720}, {557, 1720}, {655, 1720}, {773, 1720}, {863, 1720}, {970, 1720},
            {222, 1870}, {334, 1870}, {442, 1870}, {544, 1870}, {654, 1870}, {754, 1870}, {860, 1870}};

    // 联想s5 特殊字符 黑色
    private static int[][] xy_zf_s5 = new int[][]{{110, 1450}, {210, 1450}, {650, 1450}, {750, 1450}};//@#&*
    // 联想s5 特殊字符 黑色
    private static int[][] xy_zf_s5_new = new int[][]{{110, 1550}, {210, 1550}, {650, 1550}, {750, 1550}};//@#&*
    private static int[][] xy_zf;
    private static int[][] xy_abc;
    private int smsFlag = 0;
    // true为字符，false为数字
    private boolean boo = true;
    private String carrier;

    @Test
    public void demo() throws Exception {
        LogToFile.init(context, Constants.PROT);
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        // 启动银行
        Constants.scriptToStart(mDevice, "兴业银行");
        screenHeight = Constants.getScreenHeight(context, "兴业银行");
        carrier = android.os.Build.MODEL;
        if (carrier.contains("Lenovo L78011")) {
            LogToFile.d("========", "进入Z5");
            new Z5CIBbank(context, mDevice);
        } else if (carrier.contains("GOME")) {
            LogToFile.d("========", "进入M27");
            new M27CIBbank(context, mDevice);
        }
        // 获取屏幕信息
        keyPhone();
        SMSReceiverSend sMSReceiverSend = new SMSReceiverSend();
        // 注册短信广播
        Constants.registerSMSMonitor(context, sMSReceiverSend, "1069295561");
        // 动态注册sub连接状态广播
        closeNetwork();
        // 获取账户信息
        Constants.getAccountInformation(Constants.getMethod(0));
        // 触发登录页面
        triggerLandingPage(Constants.getMethod(0));
        // 登录页面
        Login();
    }

    //键盘适配
    private void keyPhone() {
        if (carrier.contains("Lenovo L58041")) {
            xy_zf = xy_zf_s5_new;
            xy_abc = xy_abc_2016_new;
            xy_Card_Number = xy_Card_Number_2016_new;
            xy_123m = xy_123m_new;
        } else {
            xy_123m = xy_123m;
            xy_zf = xy_zf_s5;
            xy_abc = xy_abc_2016;
            xy_Card_Number = xy_Card_Number_2016;
        }
    }

    /**
     * 触发登录页面
     *
     * @param method 栈的信息
     * @throws UiObjectNotFoundException UI异常
     */
    private void triggerLandingPage(String method) throws UiObjectNotFoundException {
        sleep(2000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                ChatClient.timer(method, ChatClient.errorStr);
                return;
            }
            UiObject mIV = mDevice.findObject(new UiSelector().resourceId("com.cib.cibmb:id/ivClose"));
            if(mIV.exists()){
                LogToFile.d("========", "关闭首页广告...");
                mIV.click();
                sleep(ChatClient.overtime_1);
            }
            UiObject zhuanzhang1 = mDevice.findObject(new UiSelector().text("转账汇款").className("android.widget.TextView"));
            UiObject zhuanzhang2 = mDevice.findObject(new UiSelector().descriptionContains("转账汇款").className("android.widget" +
                    ".TextView"));
            if (zhuanzhang1.exists()) {
                //进入转账
                LogToFile.d("========", "点击转账汇款1");
                zhuanzhang1.click();
                break;
            } else if (zhuanzhang2.exists()) {
                //进入转账
                LogToFile.d("========", "点击转账汇款2");
                zhuanzhang2.click();
                break;
            }
        }
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                return;
            }
            UiObject noLongerPrompt = mDevice.findObject(new UiSelector().text("不再提示").className("android.widget.TextView"));
            UiObject noLongerPrompt1 = mDevice.findObject(new UiSelector().descriptionContains("不再提示").className("android.widget.TextView"));
            if (noLongerPrompt.exists()) {
                //进入转账
                LogToFile.d("========", "不再提示");
                noLongerPrompt.click();
                break;
            } else if (noLongerPrompt1.exists()) {
                //进入转账
                LogToFile.d("========", "不再提示");
                noLongerPrompt1.click();
                break;
            }
        }
    }

    //进入登录页面补充用户信息
    private void Login() {
        sleep(2000);
        try {
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_10) {
                    LogToFile.d("========", "未找到手机号/身份证/登录名/客户号输入框");
                    break;
                }
                UiObject et = mDevice.findObject(new UiSelector().text("手机号/身份证/登录名/客户号").resourceId("com.cib.cibmb:id/edit_xingwt_accountEdit"));
                if (et.exists()) {
                    sleep(2000);
                    // 输入手机号登陆密码
                    et.setText(user.getUserName());
                    break;
                }
            }
            sleep(2000);
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_30) {
                    LogToFile.d("========", "未找到密码输入框");
                    break;
                }
                UiObject et = mDevice.findObject(new UiSelector().text("请输入登录密码").resourceId("com.cib.cibmb:id/edit_xingwt_pwEdit"));
                UiObject et1 = mDevice.findObject(new UiSelector().descriptionContains("请输入登录密码").resourceId("com.cib.cibmb:id/edit_xingwt_pwEdit"));
                if (et.exists()) {
                    et.click();
                    sleep(1000);
                    et.click();
                    sleep(2000);
                    // 输入手机号登陆密码
                    setClick_new(user.getPwd());
                    break;
                } else if (et1.exists()) {
                    et1.click();
                    sleep(1000);
                    et1.click();
                    sleep(2000);
                    // 输入手机号登陆密码
                    setClick_new(user.getPwd());
                    break;
                }
            }
            sleep(2000);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                    LogToFile.d("========", "未找到完成按钮");
                    break;
                }
                UiObject tvSure = mDevice.findObject(new UiSelector().text("完成").resourceId("com.cib.cibmb:id/tvSure"));
                UiObject tvSure1 = mDevice.findObject(new UiSelector().descriptionContains("完成").resourceId("com.cib.cibmb:id/tvSure"));
                if (tvSure.exists()) {
                    tvSure.click();
                    break;
                } else if (tvSure1.exists()) {
                    tvSure1.click();
                    break;
                }
            }
            sleep(1000);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                    LogToFile.d("========", "未找到登陆按钮");
                    break;
                }
                UiObject loginBu1 = mDevice.findObject(new UiSelector().resourceId("com.cib.cibmb:id/re_xingwt_login"));
                if (loginBu1.exists()) {
                    loginBu1.click();
                    break;
                }
            }
            sleep(5000);
            //转账金额
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_20) {
                    showLog("登录验证失败");
                    ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                    return;
                }
                UiObject webView = mDevice.findObject(new UiSelector().text("兴业手机银行").className("android.webkit.WebView"));
                if (!webView.exists()) {
                    webView = mDevice.findObject(new UiSelector().descriptionContains("兴业手机银行").className("android.webkit.WebView"));
                }
                if (webView.exists()) {
                    //登录成功
                    ChatClient.loginS();
                    sleep(4000);
                    break;
                }
            }
            setRemittance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录成功后进入转账
     */
    private void setRemittance() {
        showLog("进入等待setRemittance()");
        try {
            while (true) {
                sleep(3000);
                RUNING = list.size() > 0;
                if (RUNING) {
                    orderBankName = Constants.getBank(list.get(0).getBankName(), "CIB");
                    if (ConfirmOrder) {
                        // 查账
                        isChangeBill = true;
                        showLog("进入查账");
                        auditOrder();
                    } else {
                        // 转账
                        isChangeBill = false;
                        showLog("进入转账");
                        remittanceLogin();
                    }
                } else {
                    if (PAUSE) {
                        showLog("remittance");
                        isChangeBill = false;
                        remittance();
                    } else {
                        showLog("S_paused_C");
                        S_paused_C();
                        sleep(1000);
                    }
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 获取余额
     *
     * @throws UiObjectNotFoundException 捕获控件异常
     */
    private void getTheBalance() throws UiObjectNotFoundException {
        showLog("获取余额");
        sleep(ChatClient.overtime_5);
        boolean boo1 = false;
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            long beginTime1 = System.currentTimeMillis();
            if (beginTime1 - ChatClient.begintime > ChatClient.overtime_10) {
                showLog("转账页面刷新失败");
                boo1 = true;
                break;
            }
            clickConfirm();
            UiObject transferView7 = mDevice.findObject(new UiSelector().textContains("可用余额").className("android.view.View"));
            if (transferView7.exists()) {
                showLog("=instance(3)========" + transferView7.getText());
                UiObject mTV_money = transferView7.getFromParent(new UiSelector().className("android.view.View").index(4));
                if (mTV_money.exists()) {
                    String mMoney = mTV_money.getText();
                    if (!TextUtils.isEmpty(mMoney)  && mMoney.contains("¥")) {
                        showLog("====---mTV_money.getText()-====" + mMoney);
                        try {
                            sumMoney = mMoney.substring(mMoney.indexOf("¥") + 1);
                            sumMoney = sumMoney.replaceAll(",", "");
                            sumMoney = sumMoney.replaceAll(" ","").trim();

                            showLog("====money===:" + sumMoney);
                            if (!sumMoney.equals("")) {
                                Double sumMoney1;
                                sumMoney1 = Double.parseDouble(sumMoney);
                                C_updateBalance_W(sumMoney1);
                                sleep(3000);
                                if (!isChangeBill) {
                                    showLog("====本地保存余额====");
                                    String sumMoneyFile = user.getUserName() + bankName + "_sumMoney.txt";
                                    FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                                }
                                boo1 = false;
                                break;
                            }
                        } catch (Exception e) {
                            showLog("金额为空");
                        }
                    } else {
                        showLog("金额未刷出来");
                    }
                }
            } else {
                showLog("转账页面未刷出来");
            }
        }
        if (boo1) {
            addWorkTime((int) ChatClient.overtime_30);
            failedToObtainBalance();
            getTheBalance();
        }
    }

    private void clickConfirm() throws UiObjectNotFoundException {
        long beginTime1 = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - beginTime1) > ChatClient.overtime_5) {
                showLog("未出现异常");
                break;
            }
            UiObject button = mDevice.findObject(new UiSelector().textContains("确认").className("android.widget.Button"));
            if (button.exists()) {
                showLog("确认");
                button.click();
                break;
            }
            UiObject button1 = mDevice.findObject(new UiSelector().descriptionContains("确认").className("android.widget.Button"));
            if (button1.exists()) {
                showLog("确认");
                button1.click();
                break;
            }
        }
    }

    private void failedToObtainBalance() throws UiObjectNotFoundException {
        showLog("获取余额失败");
        sleep(1000);
        mDevice.pressBack();
        sleep(ChatClient.overtime_2);
        joinTransfer();
        sleep(ChatClient.overtime_3);
    }

    /**
     * 转账汇款
     */
    private void remittanceLogin() {
        try {
            sleep(1000);
            getTheBalance();
            addWorkTime((int) ChatClient.overtime_60);
            //收款人
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                    ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                    return;
                }
                UiObject skr = mDevice.findObject(new UiSelector().text("输入或点选\"常用收款人\" →").className("android.widget.EditText"));
                if (skr.exists()) {
                    sleep(1000);
                    skr.setText(list.get(0).getUserName());
                    break;
                }
                UiObject skr1 = mDevice.findObject(new UiSelector().descriptionContains("输入或点选\"常用收款人\" →").className("android.widget.EditText"));
                if (skr1.exists()) {
                    sleep(1000);
                    skr1.setText(list.get(0).getUserName());
                    break;
                }
            }

            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_3) {
                    showLog("无账号提示列表");
                    break;
                }
                UiObject mV_info = mDevice.findObject(new UiSelector().resourceId("payee-info").index(2));
                if (mV_info.exists()) {
                    UiObject mV1 = mV_info.getChild(new UiSelector().index(0).className("android.view.View"));
                    if (mV1.exists()) {
                        UiObject mV2 = mV1.getChild(new UiSelector().index(1).className("android.view.View"));
                        if (mV2.exists()) {
                            UiObject mV3 = mV2.getChild(new UiSelector().index(0).className("android.view.View"));
                            if (mV3.exists()) {
                                UiObject mV4 = mV3.getChild(new UiSelector().index(1).className("android.view.View"));
                                if (mV4.exists()) {
                                    UiObject mV5 = mV4.getChild(new UiSelector().index(1).className("android.view.View"));
                                    if (mV5.exists()) {
                                        showLog("点击关闭账号提示列表...");
                                        mV5.click();
                                        sleep(1000);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //账号
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                    ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                    return;
                }
                UiObject skzh = mDevice.findObject(new UiSelector().text("收款人银行卡号或账户").className("android.widget.EditText"));
                if (skzh.exists()) {
                    skzh.click();
                    sleep(1000);
                    setClickCardNumber(list.get(0).getBankNo());
                    sleep(1000);
                    mDevice.click(532, 134);
                    sleep(2000);
                    showLog("银行卡号输入完成");
                    break;
                }
                UiObject skzh1 = mDevice.findObject(new UiSelector().descriptionContains("收款人银行卡号或账户").className("android.widget.EditText"));
                if (skzh1.exists()) {
                    skzh.click();
                    sleep(1000);
                    setClickCardNumber(list.get(0).getBankNo());
                    sleep(1000);
                    mDevice.click(532, 134);
                    sleep(2000);
                    showLog("银行卡号输入完成");
                    break;
                }
            }

            // 关闭最佳匹配
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_2) {
                    showLog("检测关闭");
                    break;
                }
                UiObject bestFit = mDevice.findObject(new UiSelector().text("最佳匹配").className("android.view.View"));
                if (bestFit.exists()) {
                    UiObject close = mDevice.findObject(new UiSelector().text("关闭").className("android.view.View"));
                    if (close.exists()) {
                        showLog("关闭");
                        close.click();
                        break;
                    }
                }
                UiObject bestFit1 = mDevice.findObject(new UiSelector().descriptionContains("最佳匹配").className("android.view.View"));
                if (bestFit1.exists()) {
                    UiObject close = mDevice.findObject(new UiSelector().descriptionContains("关闭").className("android.view.View"));
                    if (close.exists()) {
                        showLog("关闭");
                        close.click();
                        break;
                    }
                }
            }
            sleep(ChatClient.overtime_2);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_3) {
                    showLog("银行名错误超时");
                    break;
                }
                UiObject mET_bankName = mDevice.findObject(new UiSelector().className("android.widget.EditText").text("收款银行卡所属银行"));
                if (mET_bankName.exists()) {
                    UiObject view1 = mDevice.findObject(new UiSelector().resourceId("payee-info"));
                    UiObject view2 = view1.getChild(new UiSelector().className("android.view.View").index(2));
                    UiObject view3 = view2.getChild(new UiSelector().className("android.view.View").index(2));
                    if (view3.exists()) {
                        view3.click();
                        sleep(ChatClient.overtime_5);
                        UiObject mEditText_bankName = mDevice.findObject(new UiSelector().className("android.widget.EditText").text("输入银行名称查询"));
                        if (mEditText_bankName.exists()) {
                            mEditText_bankName.setText(orderBankName);
                        }
                        sleep(ChatClient.overtime_5);
                        UiObject mButton_bankName = mDevice.findObject(new UiSelector().className("android.view.View").text(orderBankName));
                        if (mButton_bankName.exists()) {
                            mButton_bankName.click();
                        } else {
                            String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                                    "'phoneModel':'兴业银行'," +
                                    "'info':'银行卡号或银行名错误'";
                            String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1," +
                                    "'type':1";
                            failureToReport(json, mobileD);
                            joinTransfer();
                            return;
                        }
                    } else {
                        String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                                "'phoneModel':'兴业银行'," +
                                "'info':'银行卡号或银行名错误'";
                        String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1," +
                                "'type':1";
                        failureToReport(json, mobileD);
                        joinTransfer();
                        return;
                    }
                }
            }
            sleep(ChatClient.overtime_2);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                    ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                    break;
                }
                UiObject button = mDevice.findObject(new UiSelector().text("下一步").className("android.widget.Button"));
                if (button.exists()) {
                    showLog("下一步");
                    button.click();
                    break;
                }
                UiObject button1 = mDevice.findObject(new UiSelector().descriptionContains("下一步").className("android.widget.Button"));
                if (button1.exists()) {
                    showLog("下一步");
                    button1.click();
                    break;
                }
            }
            sleep(3000);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                    ChatClient.timer(Constants.getMethod(0), "转账信息填充错误");
                    return;
                }
                UiObject mTV = mDevice.findObject(new UiSelector().resourceId("ui-tran-card"));
                if (mTV.exists()) {
                    UiObject mTV_name = mTV.getChild(new UiSelector().index(0).className("android.view.View"));
                    if (mTV_name.exists()) {
                        String nameText = mTV_name.getText();
                        if (StringUtils.isEmpty(nameText)) {
                            nameText = mTV_name.getContentDescription();
                        }
                        showLog(nameText);
                        if (nameText.contains(list.get(0).getUserName())) {
                            showLog("账户正确");
                            break;
                        }
                    }
                }
            }
            EnterTheAmount();
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                    ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                    break;
                }
                UiObject button = mDevice.findObject(new UiSelector().text("下一步").className("android.widget.Button"));
                UiObject button1 = mDevice.findObject(new UiSelector().descriptionContains("下一步").className("android.widget.Button"));
                if (button.exists()) {
                    showLog("下一步");
                    button.click();
                    break;
                }
                if (button1.exists()) {
                    showLog("下一步");
                    button1.click();
                    break;
                }
            }
            sleep(2000);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                    ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                    break;
                }
                UiObject button = mDevice.findObject(new UiSelector().text("确认转账").className("android.widget.Button"));
                if (button.exists()) {
                    showLog("确认转账1");
                    button.click();
                    break;
                }
                UiObject button1 = mDevice.findObject(new UiSelector().descriptionContains("确认转账").className("android.widget.Button"));
                if (button1.exists()) {
                    showLog("确认转账2");
                    button1.click();
                    break;
                }
            }
            sleep(ChatClient.overtime_5);
            addWorkTime((int) ChatClient.overtime_60);
            isFace();
            /*
             * 处理短信与转账密码
             */
            smsFlag = 0;
            SMSOrPwd();
            /*
             * 结果页处理
             */
            orderFinish();
        } catch (Exception e) {
            e.printStackTrace();
            LogToFile.d("====结果页====", "====结果页错误====" + e.getMessage());
            list.clear();
        }
    }

    private String digitalCode = "0";

    private void isFace() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_3) {
                mDevice.click(64, 133);
                mDevice.click(64, 133);
                digitalVerification();
                return;
            }
            UiObject button = mDevice.findObject(new UiSelector().text("短信口令").className("android.widget.TextView"));
            if (button.exists()) {
                showLog("不是人脸识别");
                break;
            }
        }
    }

    private void digitalVerification() throws UiObjectNotFoundException {
        sleep(5000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_8) {
                showLog("立即验证超时...");
                break;
            }
            UiObject button = mDevice.findObject(new UiSelector().textContains("立即验证").className("android.widget.Button"));
            if (button.exists()) {
                button.click();
                showLog("立即验证");
                mDevice.click(754, 1291);
                mDevice.click(754, 1291);
                break;
            }
        }
        sleep(2000);
        digitalCode = "0";
        while (true) {
            UiObject textView = mDevice.findObject(new UiSelector().textContains("请输入理财卡").className("android.view.View"));
            if (textView.exists()) {
                showLog("获取数字验证需要第几位");
                String digitalString = textView.getText();
                digitalCode = digitalString.substring(digitalString.indexOf("第"));
                showLog("digitalCode==" + digitalCode);
                digitalCode = digitalCode.substring(1, 2);
                showLog("digitalCode==" + digitalCode);
                if (isNumeric(digitalCode)) {
                    break;
                } else {
                    mDevice.pressBack();
                    sleep(ChatClient.overtime_5);
                    mDevice.pressBack();
                    isFace();
                    return;
                }

            }
        }
        sleep(1000);
        while (true) {
            UiObject mV_cardCode = mDevice.findObject(new UiSelector().resourceId("cardCode").index(4));
            UiObject listView = mV_cardCode.getChild(new UiSelector().className("android.widget.ListView").index(0));
            if (listView.exists()) {
                showLog("点击输入框");
                listView.click();
                break;
            }
        }
        sleep(3000);
        //        user.setUsbPayPwd("260497428");
        if (user.getUsbPayPwd() != null && !(user.getUsbPayPwd().isEmpty())) {
            showLog("digitalCode==" + digitalCode);
            String usbPayPwd = user.getUsbPayPwd().substring(Integer.valueOf(digitalCode) - 1, Integer.valueOf(digitalCode) + 3);
            showLog("usbPayPwd==" + usbPayPwd);
            String bankName = "CIB2CODE";
            if (screenHeight == 2016) {
                getJpImg(usbPayPwd, bankName);
                sleep(2000);
                LogToFile.d("", "======================--------->");
                mDevice.click(1000, 1250);
            } else if (screenHeight == 1920) {
                getJpImg(usbPayPwd, bankName);
                sleep(2000);
                mDevice.click(1000, 1200);
            } else if (screenHeight == 1440) {
                getJpImg(usbPayPwd, bankName);
                sleep(2000);
                mDevice.click(745, 900);
            } else {
                getJpImg(usbPayPwd, bankName);
                sleep(2000);
                LogToFile.d("", "==============new s5========--------->");
                mDevice.click(1000, 1250);
            }
            sleep(1000);
        }
        while (true) {
            UiObject button = mDevice.findObject(new UiSelector().textContains("确认提交").className("android.widget.Button"));
            if (button.exists()) {
                showLog("确认提交");
                button.click();
                break;
            }
        }
    }


    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    private void EnterTheAmount() throws UiObjectNotFoundException {
        //转账金额
        while (true) {
            UiObject cardSum = mDevice.findObject(new UiSelector().resourceId("ui-card-sum").index(2));
            UiObject cardSumView = cardSum.getChild(new UiSelector().className("android.view.View").index(2));
            UiObject cardSumView1 = cardSumView.getChild(new UiSelector().className("android.view.View").index(0));
            UiObject cardSumView2 = cardSumView1.getChild(new UiSelector().className("android.view.View").index(0));
            UiObject cardSumView3 = cardSumView2.getChild(new UiSelector().className("android.view.View").index(1));
            UiObject editTextView = cardSumView3.getChild(new UiSelector().className("android.widget.EditText").index(0));
            if (editTextView.exists()) {
                editTextView.click();
                break;
            } else {
                UiObject cardSumView4 = cardSum.getChild(new UiSelector().className("android.view.View").index(3));
                UiObject cardSumView5 = cardSumView4.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject cardSumView6 = cardSumView5.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject cardSumView7 = cardSumView6.getChild(new UiSelector().className("android.view.View").index(1));
                editTextView = cardSumView7.getChild(new UiSelector().className("android.widget.EditText").index(0));
                if (editTextView.exists()) {
                    editTextView.click();
                    break;
                }
            }
        }
        sleep(2000);
        // 设置金额
        setClickM(list.get(0).getMoney());
        sleep(1000);
        // 点击完成
        mDevice.click(930, 1840);
        sleep(2000);
        AmountOfContrast();
    }

    private void AmountOfContrast() throws UiObjectNotFoundException {
        showLog("订单金额与输入金额对比");
        while (true) {
            UiObject cardSum = mDevice.findObject(new UiSelector().resourceId("ui-card-sum").index(2));
            UiObject cardSumView = cardSum.getChild(new UiSelector().className("android.view.View").index(2));
            UiObject cardSumView1 = cardSumView.getChild(new UiSelector().className("android.view.View").index(0));
            UiObject cardSumView2 = cardSumView1.getChild(new UiSelector().className("android.view.View").index(0));
            UiObject cardSumView3 = cardSumView2.getChild(new UiSelector().className("android.view.View").index(1));
            UiObject editTextView = cardSumView3.getChild(new UiSelector().className("android.widget.EditText").index(1));
            String money = null;
            if (editTextView.exists()) {
                money = editTextView.getText();
            } else {
                UiObject cardSumView4 = cardSum.getChild(new UiSelector().className("android.view.View").index(3));
                UiObject cardSumView5 = cardSumView4.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject cardSumView6 = cardSumView5.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject cardSumView7 = cardSumView6.getChild(new UiSelector().className("android.view.View").index(1));
                editTextView = cardSumView7.getChild(new UiSelector().className("android.widget.EditText").index(1));
                if (editTextView.exists()) {
                    money = editTextView.getText();
                }
            }
            if (!TextUtils.isEmpty(money)) {
                if (Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(money.replaceAll(",", ""))) {
                    showLog("订单金额==" + list.get(0).getMoney() + "；" + "输入金额==" + editTextView.getText() + "--->金额相等");
                } else {
                    int length = editTextView.getText().length() + 1;
                    showLog("订单金额==" + list.get(0).getMoney() + "；" + "输入金额==" + editTextView.getText() + "--->金额不相等");
                    sleep(1000);
                    editTextView.click();
                    sleep(2000);
                    for (int i = 0; i < length; i++) {
                        mDevice.click(930, 1500);
                        sleep(500);
                    }
                    setClickM(list.get(0).getMoney());
                    sleep(1000);
                    // 点击完成
                    mDevice.click(930, 1840);
                    sleep(2000);
                    AmountOfContrast();
                }
                break;
            }
        }
    }

    /**
     * 转账结果页
     */
    private void orderFinish() throws UiObjectNotFoundException {
        sleep(1000);
        //取款密码输入
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 3000) {
                break;
            }
            UiObject bpwd = mDevice.findObject(new UiSelector().resourceId("com.cib.cibmb:id/point"));
            if (bpwd.exists()) {
                sleep(1000);
                LogToFile.d("---截取取款密码的键盘---", "-----截取取款密码的键盘-----");
                // 收起键盘
                String bankName = "CIB";
                if (screenHeight == 2016) {
                    getJpImg(user.getBankpwd(), bankName);
                    sleep(ChatClient.overtime_1);
                    LogToFile.d("", "======================--------->");
                    mDevice.click(1000, 1250);
                } else if (screenHeight == 1920) {
                    getJpImg(user.getBankpwd(), bankName);
                    sleep(ChatClient.overtime_1);
                    mDevice.click(1000, 1200);
                } else if (screenHeight == 1440) {
                    getJpImg(user.getBankpwd(), bankName);
                    sleep(ChatClient.overtime_1);
                    mDevice.click(745, 900);
                } else {
                    getJpImg(user.getBankpwd(), bankName);
                    sleep(ChatClient.overtime_1);
                    LogToFile.d("", "==============new s5========--------->");
                    mDevice.click(1000, 1250);
                }
                sleep(1000);
                break;
            }
        }

        sleep(3000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_60) {
                showLog("软令牌同意认证1");
                mDevice.click(500, 1510);
                break;
            }

            UiObject mV = mDevice.findObject(new UiSelector().text("同意认证"));
            if (!mV.exists()) {
                mV = mDevice.findObject(new UiSelector().descriptionContains("同意认证"));
            }
            if (mV.exists()) {
                showLog("软令牌同意认证2");
                mV.click();
                sleep(ChatClient.overtime_5);
            }
            UiObject state = mDevice.findObject(new UiSelector().text("完成").index(2));
            if (state.exists()) {
                LogToFile.d("====结果页====", "====跳转到结果页====");
                sleep(2000);
                getUserInfo();
//                state.click();
                mDevice.click(980, 140);
                mDevice.click(980, 140);
                //删除处理过的订单
                mapDelete(list.get(0).getOrder());
                getmap();
                sleep(1000);
                //返回
                remittance();
                break;
            }
        }
    }

    /**
     * 短信与转账密码处理
     */
    private void SMSOrPwd() throws UiObjectNotFoundException {
        addWorkTime((int) ChatClient.overtime_90);
        if (user.getUseType().equals("1")) {
            showLog("处理转账短信---真机");
            //10.1.0版本没有
            setPhoneSMS();
        } else {
            showLog("处理转账短信---无SIM卡");
            setNotSIM_SMS();
        }
        sleep(ChatClient.overtime_3);
        errorSMSValidation();
    }

    private void errorSMSValidation() throws UiObjectNotFoundException {
        addWorkTime((int) ChatClient.overtime_60);
        /*
         * 转账错误处理
         */
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            long nowTime1 = System.currentTimeMillis();
            if (nowTime1 - ChatClient.begintime > ChatClient.overtime_60) {
                showLog("errorSMSValidation账单错误超时");
                break;
            }
            UiObject bpwd = mDevice.findObject(new UiSelector().resourceId("com.cib.cibmb:id/point"));
            if (bpwd.exists()) {
                return;
            }
            UiObject mTV_end = mDevice.findObject(new UiSelector().text("完成").className("android.view.View"));
            if (mTV_end.exists()) {
                showLog("进入结果页");
                return;
            }
            String errorMsg = errorMsg();
            if (!StringUtils.isEmpty(errorMsg)) {
                showLog("StringUtils.isEmpty(errorMsg)：" + errorMsg);
                if (errorMsg.contains("错误")) {
                    showLog("验证码输入错误次数：" + smsFlag);
                    sleep(ChatClient.overtime_20);
                    if (smsFlag < 3) {
                        while (true) {
                            UiObject mBT = mDevice.findObject(new UiSelector().resourceId("com.cib.cibmb:id/tvCountdown").text("重新发送"));
                            if (mBT != null && mBT.exists()) {
                                showLog("点击确认");
                                mBT.click();
                                sleep(ChatClient.overtime_3);
                                smsFlag += 1;
                                SMSOrPwd();
                                return;
                            }
                        }
                    } else {
                        ChatClient.timer(Constants.getMethod(0), "短息获取超过三次");
                        return;
                    }
                } else {
                    ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
                    String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':4,'type':1";
                    String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                            "'phoneModel':'兴业银行','info':'短信获取失败'";
                    failureToReport(json, mobileD);
                    sleep(1000);
                    mDevice.pressBack();
                    return;
                }
            }
        }
    }

    private String errorMsg() throws UiObjectNotFoundException {
        UiObject errorMsgTitle = mDevice.findObject(new UiSelector().resourceId("com.cib.cibmb:id/centerBottom"));
        String errorResult;
        if (errorMsgTitle.exists()) {
            errorResult = errorMsgTitle.getText();
            return errorResult;
        }
        return "";
    }

    /**
     * 处理无SIM卡手机短信逻辑
     */
    private void setNotSIM_SMS() {
        if (smsFlag > 3) {
            ChatClient.timer(LogToFile.getMethodName(), "短信验证码超时错误");
            return;
        }
        sleep(ChatClient.overtime_10);
        SMSCode smsCode = requestSMSCode(user.getUserName(), user.getBankId());
        if (StringUtils.isEmpty(smsCode.getSmscode())) {
            ChatClient.timer(LogToFile.getMethodName(), "获取不到验证码");
        } else {
            setClickCardNumber(smsCode.getSmscode());
        }
    }

    /**
     * 监听短信点击输入验证码
     */
    private void setPhoneSMS() {
        //判断页面是否跳转
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_10) {
                ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                return;
            }
            UiObject tvInput = mDevice.findObject(new UiSelector().resourceId("com.cib.cibmb:id/singleTitle"));
            if (tvInput.exists()) {
                showLog("短信口令页面");
                sleep(2000);
                break;
            }
        }
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            long nowTime1 = System.currentTimeMillis();
            if (nowTime1 - ChatClient.begintime > ChatClient.overtime_60) {
                ChatClient.timer(LogToFile.getMethodName(), "短信验证码超时错误");
                break;
            }
            if (TextUtils.isEmpty(Constants.CODE)) {
                sleep(3000);
            } else {
                showLog("返回短信验证码为：" + Constants.CODE);
                break;
            }
        }
        sleep(3000);
        setClickCardNumber(Constants.CODE);
        Constants.CODE = null;
    }


    /**
     * 字母和数字键盘的切换
     *
     * @param checkStr 字符串
     * @return true字母  false特殊字符
     */
    private boolean isNumericZF(String checkStr) {
        return checkStr.contains("#") || checkStr.contains("*") || checkStr.contains("&") || checkStr.contains("@");
    }

    /**
     * 登陆密码
     *
     * @param pad 登陆密码
     */
    private void setClick_new(String pad) {
        //        LogToFile.d("====输入手机APK登陆密码====", "===输入手机APK登陆密码===" + pad);
        Exception exception = new Exception();
        StackTraceElement[] trace = exception.getStackTrace();
        String method = trace[0].getMethodName();
        try {
            char[] abc = "qwertyuiopasdfghjklzxcvbnm".toCharArray();//char数组
            char[] zf = "@#&*".toCharArray();//char数组
            char[] pwd = pad.toCharArray();//char数组
            char[] num_abc;//char数组
            sleep(2000);
            for (char c : pwd) {
                if (isNumericZF(String.valueOf(c))) {
                    num_abc = zf;
                } else {
                    num_abc = abc;
                }
                for (int i = 0; i < num_abc.length; i++) {
                    if (num_abc[i] == c) {
                        if (isNumericZF(String.valueOf(c))) {
                            if (boo) {
                                sleep(1000);
                                clickSpecialCharacter();
                            }
                            sleep(1000);
                            mDevice.click(xy_zf[i][0], xy_zf[i][1]);
                        } else {
                            if (!boo) {
                                sleep(1000);
                                clickSpecialCharacter();
                            }
                            sleep(1000);
                            mDevice.click(xy_abc[i][0], xy_abc[i][1]);

                        }
                        break;
                    }
                }
            }
        } catch (Exception e) {
            showLog("点击特殊字符=======为空");
            ChatClient.timer(method, "");
        }
    }

    /**
     * 点击特殊字符
     */
    private void clickSpecialCharacter() {
        if (screenHeight == 2016) {
            mDevice.click(950, 1940);
        } else if (screenHeight == 1920) {
            mDevice.click(950, 1850);
        } else {
            mDevice.click(950, 2030);
        }
        boo = !boo;
    }

    /**
     * 模拟点击密码
     *
     * @param pad_ 要点击的
     */
    private void setClick_pwd(List<PointBean> pointBeanList, String pad_) {
        StringBuilder tempStr = new StringBuilder();
        if (pointBeanList.size() > 0) {
            for (int i = 0; i < pointBeanList.size(); i++) {
                tempStr.append(pointBeanList.get(i).getCharStr());
            }
        }
        char[] abc_num = tempStr.toString().toCharArray();//字母或者数字数组
        char[] pwd = pad_.toCharArray();//要点击的密码char数组
        for (char c : pwd) {
            sleep(1000);
            for (int i = 0; i < abc_num.length; i++) {
                if (abc_num[i] == c) {
                    sleep(1000);
                    //点击数字
                    int num = pointBeanList.get(i).getY();
                    if (screenHeight == 2016) {
                        mDevice.click((pointBeanList.get(i).getX()), num + 1380);
                    } else if (screenHeight == 1920) {
                        mDevice.click((pointBeanList.get(i).getX()), num + 840);
                    } else if (screenHeight == 1280) {
                        mDevice.click((pointBeanList.get(i).getX()), num + 840);
                    } else {
                        mDevice.click((pointBeanList.get(i).getX()), num + 1460);
                    }
                    Log.d("====---点击数字密码====", String.valueOf(c));
                }
            }
        }
    }

    /**
     * 境内汇款页面
     */
    private void remittance() {
        try {
            while (true) {
                UiObject skr = mDevice.findObject(new UiSelector().className("android.widget.EditText").index(0).text("收款人银行卡号或账户"));
                if (skr.exists()) {
                    showLog("检测进入已转账页面");
                    break;
                }
                UiObject skr1 = mDevice.findObject(new UiSelector().className("android.widget.EditText").index(0).descriptionContains("收款人银行卡号或账户"));
                if (skr1.exists()) {
                    showLog("检测进入已转账页面");
                    break;
                }
            }
            //进入转账页面
            getTheBalance();
            LogToFile.d("请求订单", "请求订单");
            ChatClient.request();
            if (ChatClient.waitExistOrder(ChatClient.overtime_60)) {
                orderBankName = Constants.getBank(list.get(0).getBankName(), "CIB");
                // 转账
                showLog("进入转账");
                remittanceLogin();
                return;
            }
            mDevice.pressBack();
            if (ChatClient.existOrder()) {
                sleep(ChatClient.overtime_2);
            } else {
                sleep(ChatClient.overtime_5);
            }
            joinTransfer();

        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer("remittance", ChatClient.error);
            e.printStackTrace();
        }
    }

    /**
     * 查账
     */
    private void auditOrder() {
        try {
            sleep(3000);
            //进入转账页面
            getTheBalance();
            while (true) {
                UiObject webView = mDevice.findObject(new UiSelector().text("兴业手机银行").className("android.webkit.WebView"));
                if (!webView.exists()) {
                    webView = mDevice.findObject(new UiSelector().descriptionContains("兴业手机银行").className("android.webkit.WebView"));
                }
                UiObject transferView1 = webView.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject transferView2 = transferView1.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject transferView3 = transferView2.getChild(new UiSelector().className("android.view.View").index(0));
                UiObject transferView4 = transferView3.getChild(new UiSelector().className("android.view.View").index(0));
                if (screenHeight == 2016) {
                    UiObject transferView5 = transferView4.getChild(new UiSelector().className("android.view.View").index(0));
                    UiObject transferView6 = transferView5.getChild(new UiSelector().className("android.view.View").index(2));
                    if (transferView6.exists()) {
                        showLog("点击...展开view");
                        //查账
                        transferView6.click();
                        break;
                    }
                } else if (screenHeight == 1920) {
                    UiObject transferView5 = transferView4.getChild(new UiSelector().className("android.view.View").index(2));
                    if (transferView5.exists()) {
                        showLog("点击...展开view");
                        //查账
                        transferView5.click();
                        break;
                    }
                } else {
                    UiObject transferView5 = transferView4.getChild(new UiSelector().className("android.view.View").index(0));
                    UiObject transferView6 = transferView5.getChild(new UiSelector().className("android.view.View").index(2));
                    if (transferView6.exists()) {
                        showLog("点击...展开view");
                        //查账
                        transferView6.click();
                        break;
                    }
                }

            }
            sleep(1000);
            while (true) {
                UiObject listView = mDevice.findObject(new UiSelector().className("android.widget.ListView").index(1));
                if (!listView.exists()) {
                    //1920使用
                    listView = mDevice.findObject(new UiSelector().className("android.widget.ListView").index(0));
                }
                if (listView.exists()) {
                    showLog("listView....");
                    UiObject transferRecord = listView.getChild(new UiSelector().text("转账记录").className("android.view.View").index(0));
                    if (transferRecord.exists()) {
                        showLog("点击转账记录");
                        //查询
                        transferRecord.click();
                        break;
                    }
                }
            }
            sleep(3000);
            while (true) {
                UiObject listView = mDevice.findObject(new UiSelector().className("android.widget.ListView").index(1));
                if (!listView.exists()) {
                    //1920使用
                    listView = mDevice.findObject(new UiSelector().className("android.widget.ListView").index(0));
                }
                if (listView.exists()) {
                    showLog("有转账记录");
                    UiObject transferRecord = listView.getChild(new UiSelector().className("android.view.View").index(1));
                    if (transferRecord.exists()) {
                        showLog("点击最近一条转账记录");
                        UiObject transferRecord1 = transferRecord.getChild(new UiSelector().className("android.view.View").index(1));
                        if (transferRecord1.exists()) {
                            //查询
                            transferRecord1.click();
                        }
                        break;
                    }
                } else {
                    showLog("没有记录");
                    String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                    String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                            "'phoneModel':'兴业银行','info':'查询超时'";
                    failureToReport(json, mobileD);
                    return;
                }
            }

            sleep(ChatClient.overtime_3);
            String money;
            String userName;
            String bankNo;
            String status;
            //获取交易记录
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                sleep(1000);
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > 30000) {
                    String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':6,'type':1";
                    String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                            "'phoneModel':'兴业银行','info':'查询超时'";
                    failureToReport(json, mobileD);
                    sleep(1000);
                    mDevice.pressBack();
                    sleep(1000);
                    return;
                }
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    long nowtime1 = System.currentTimeMillis();
                    if ((nowtime1 - ChatClient.begintime) > ChatClient.overtime_10) {
                        showLog("账单为空超时，继续查询");
                        break;
                    }
                    UiObject mTV = mDevice.findObject(new UiSelector().text("未查询到转账记录。").className("android.widget.TextView"));
                    if (!mTV.exists()) {
                        mTV = mDevice.findObject(new UiSelector().descriptionContains("未查询到转账记录。").className("android.widget.TextView"));
                    }
                    if (mTV.exists()) {
                        String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':4,'type':1";
                        String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                                "'phoneModel':'兴业银行'," +
                                "'info':'交易失败'";
                        ChatClient.msg("<YZF>{'header':'" + ChatClient.S_CONFIRM_TRADE_RESULT_C + "'," + json + "," + mobileD + "}</YZF>");
                        getmap();
                        sleep(2000);
                        back();
                        sleep(1000);
                        mDevice.pressBack();
                        return;
                    }
                }
                UiObject checkAccounts = mDevice.findObject(new UiSelector().className("android.widget.ListView").index(0));
                if (checkAccounts.exists()) {
                    showLog("有listView");
                    //查询
                    UiObject textView1 = checkAccounts.getChild(new UiSelector().className("android.view.View").index(1));
                    UiObject textView2 = textView1.getChild(new UiSelector().className("android.view.View").index(1));
                    UiObject textView3 = checkAccounts.getChild(new UiSelector().className("android.view.View").index(4));
                    UiObject textView4 = textView3.getChild(new UiSelector().className("android.view.View").index(1));
                    UiObject textView5 = checkAccounts.getChild(new UiSelector().className("android.view.View").index(2));
                    UiObject textView6 = textView5.getChild(new UiSelector().className("android.view.View").index(1));
                    UiObject textView7 = checkAccounts.getChild(new UiSelector().className("android.view.View").index(9));
                    if (!textView7.exists()) {
                        textView7 = checkAccounts.getChild(new UiSelector().className("android.view.View").index(8));
                    }
                    UiObject textView8 = textView7.getChild(new UiSelector().className("android.view.View").index(1));
                    if (textView2.exists() && textView4.exists() && textView6.exists() && textView8.exists()) {
                        showLog("textView2.exists() && textView4.exists()不为空");
                        userName = textView2.getText();
                        money = textView4.getText().substring(textView4.getText().indexOf("¥") + 1);
                        bankNo = textView6.getText().substring(textView6.getText().length() - 4);
                        status = textView8.getText();
                        break;
                    }
                }
            }
            String json = null;
            String mobileD = null;
            String moery1 = money.replaceAll(",", "");
            System.out.println(Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(moery1));

            if (Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(moery1) && userName.contains(list.get(0)
                    .getUserName()) && list.get(0).getBankNo().contains(bankNo) && Constants.getStr(status) == 0) {
                json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':0,'type':1";
                mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行'," +
                        "'info':'交易成功'";
            } else {
                String sumMoneyFile = user.getUserName() + bankName + "_sumMoney.txt";
                String oldMoney = FileHelper.readSDFile(sumMoneyFile);
                switch (Constants.changeInAmount(bankName, oldMoney, sumMoney, list.get(0).getMoney())) {
                    case "0":
                        json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':0,'type':1";
                        mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行'," +
                                "'info':'交易成功'";
                        break;
                    case "1":
                        json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                        mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行'," +
                                "'info':'交易待处理...'";
                        break;
                    case "4":
                        json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':4,'type':1";
                        mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行'," +
                                "'info':'未查到条目'";
                        break;
                }
            }
            failureToReport(json, mobileD);
            sleep(1000);
            mDevice.pressBack();
        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer("auditOrder", ChatClient.error);
            e.printStackTrace();
        }
    }

    private void failureToReport(String json, String mobileD) {
        ChatClient.msg("<YZF>{'header':'" + ChatClient.S_CONFIRM_TRADE_RESULT_C + "'," + json + "," + mobileD + "}</YZF>");
        //删除处理过的订单
        mapDelete(list.get(0).getOrder());
        getmap();
        sleep(ChatClient.overtime_1);
        back();
    }

    private void joinTransfer() throws UiObjectNotFoundException {
        LogToFile.d("========", "点击转账汇款1");
        while (true) {
            UiObject zhuanzhang1 = mDevice.findObject(new UiSelector().text("转账汇款").className("android.widget.TextView"));
            UiObject zhuanzhang2 = mDevice.findObject(new UiSelector().descriptionContains("转账汇款").className("android.widget" +
                    ".TextView"));
            if (zhuanzhang1.exists()) {
                //进入转账
                LogToFile.d("========", "点击转账汇款1");
                zhuanzhang1.click();
                break;
            } else if (zhuanzhang2.exists()) {
                //进入转账
                LogToFile.d("========", "点击转账汇款2");
                zhuanzhang2.click();
                break;
            }
            sleep(ChatClient.overtime_3);
        }
    }

    /**
     * 兴业实时监测网络异常处理
     */
    private void closeNetwork() {
        new Thread() {
            public void run() {
                try {
                    while (true) {
                        UiObject network0 = mDevice.findObject(new UiSelector().text("是").className("android.widget.Button").index(1));
                        UiObject network1 = mDevice.findObject(new UiSelector().descriptionContains("是").className("android.widget" +
                                ".Button").index(1));
                        UiObject network2 = mDevice.findObject(new UiSelector().text("否").className("android.widget.Button").index(3));
                        UiObject network3 = mDevice.findObject(new UiSelector().descriptionContains("否").className("android.widget" +
                                ".Button").index(3));
                        UiObject network4 = mDevice.findObject(new UiSelector().text("确定").className("android.widget.Button").index(1));
                        UiObject network5 = mDevice.findObject(new UiSelector().descriptionContains("确定").className("android.widget" +
                                ".Button").index(1));
                        // LogToFile.d("===监测网络===","=======监测网络=====");
                        if (network0.exists()) {
                            LogToFile.d("======", "=======networkError=====");
                            network0.click();
                            sleep(3000);
                        } else if (network1.exists()) {
                            LogToFile.d("======", "=======networkError=====");
                            network1.click();
                            sleep(3000);
                        } else if (network2.exists()) {
                            LogToFile.d("======", "=======networkError=====");
                            network2.click();
                            sleep(3000);
                        } else if (network3.exists()) {
                            LogToFile.d("======", "=======networkError=====");
                            network3.click();
                            sleep(3000);
                        } else if (network4.exists()) {
                            LogToFile.d("======", "=======networkError=====");
                            network4.click();
                            sleep(3000);
                        } else if (network5.exists()) {
                            LogToFile.d("======", "=======networkError=====");
                            network5.click();
                            sleep(3000);
                        }
                        sleep(5000);
                    }
                } catch (Exception e) {
                    LogToFile.d("closeNetwork", "=======closeNetwork=====");
                    setDevicesLog(e.getCause().getMessage());
                    e.printStackTrace();
                }

            }
        }.start();
    }

    /**
     * 完成转账后回复服务端
     */
    private void getUserInfo() {
        Exception exception = new Exception();
        StackTraceElement[] trace = exception.getStackTrace();
        String method = trace[0].getMethodName();
        try {
            String balance;
            String type;
            showLog("获取结果页信息");
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_30) {
                    showLog("结果页超时");
                    ChatClient.timer(method, "结果页超时");
                    return;
                }
                //  获取交易结果 兴业银行无交易完成，交易中。。。。只有交易已提交到对方账户  梁钊
                UiObject stateFailure = mDevice.findObject(new UiSelector().resourceId("ui-real-result"));
                if (stateFailure.exists()) {
                    balance = sumMoney;
                    type = "失败";
                    UiObject stateFailure1 = stateFailure.getChild(new UiSelector().className("android.view.View").text("转账失败"));
                    if (stateFailure1.exists()) {
                        String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行'," +
                                "'info':'" + type + "'";
                        String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + balance + "','state':" + Constants.getStr(type)
                                + ",'type':1";
                        ChatClient.msg("<YZF>{'header':'" + ChatClient.M_SEND_TRADE_RESULT_P + "'," + json + "," + mobileD + "}</YZF>");
                        return;
                    }
                }

                //  获取交易结果 兴业银行无交易完成，交易中。。。。只有交易已提交到对方账户  梁钊
                UiObject state = mDevice.findObject(new UiSelector().resourceId("TransferSuccess"));
                UiObject state1;
                UiObject state2 = null;
                if (state.exists()) {
                    showLog("获取结果页信息  state  不为空");
                    state1 = state.getChild(new UiSelector().className("android.view.View").index(0));
                    if (state1.exists()) {
                        showLog("获取结果页信息  state1  不为空");
                        state2 = state1.getChild(new UiSelector().className("android.view.View").index(1).text("资金已转入对方账户"));
                    }
                }
                if (state2 != null && state.exists() && !TextUtils.isEmpty(state2.getText())) {
                    showLog("获取结果页信息  state2  不为空");
                    if (state2.getText().contains("资金已转入对方账户")) {
                        DecimalFormat formater = new DecimalFormat("#0.##");
                        balance = formater.format(Double.parseDouble(sumMoney) - Double.parseDouble(list.get(0).getMoney()));
                        showLog("保存余额...");
                        String sumMoneyFile = user.getUserName() + bankName + "_sumMoney.txt";
                        FileHelper.saveMoneySDFile(false, balance, sumMoneyFile);
                        type = "成功";
                    } else {
                        balance = sumMoney;
                        type = "失败";
                    }
                    String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'兴业银行'," +
                            "'info':'" + type + "'";
                    String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + balance + "','state':" + Constants.getStr(type)
                            + ",'type':1";
                    ChatClient.msg("<YZF>{'header':'" + ChatClient.M_SEND_TRADE_RESULT_P + "'," + json + "," + mobileD + "}</YZF>");
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟点击金额
     */
    private void setClickM(String money_) {
        try {
            char[] num = "123456789.0".toCharArray();//char数组
            char[] money = money_.toCharArray();//char数组

            for (char c : money) {
                for (int i = 0; i < num.length; i++) {
                    if (num[i] == c) {
                        mDevice.click(xy_123m[i][0], xy_123m[i][1]);
                        sleep(1000);
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟输入账户
     */
    private void setClickCardNumber(String money_) {
        try {
            char[] num = "1234567890".toCharArray();//char数组
            char[] money = money_.toCharArray();//char数组

            for (char c : money) {
                for (int i = 0; i < num.length; i++) {
                    if (num[i] == c) {
                        mDevice.click(xy_Card_Number[i][0], xy_Card_Number[i][1]);
                        sleep(500);
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 截去英文键盘模拟用户输入密码
     * U盾密码字母键盘
     *
     * @param pwd:要点击的密码
     */
    private void getJpImg(String pwd, String bankName) {

        List<PointBean> pointBeanList;
        try {
            LogToFile.d("========", "截图=" + screenHeight);
            Bitmap bitmap = null;
            if (screenHeight == 2016) {
                bitmap = ImageView.getImage1(0, 1367, 1080, 2014, 1080, 647);
            } else if (screenHeight == 1920) {
                bitmap = ImageView.getImage1(0, 1013, 720, 1344, 1080, 331);
            } else if (screenHeight == 1280) {
                bitmap = ImageView.getImage1(0, 850, 720, 1260, 720, 410);
            } else {
                bitmap = ImageView.getImage1(0, 1453, 1080, 2100, 1080, 647);
            }
            pointBeanList = saveImg22(bitmap, bankName);
            setClick_pwd(pointBeanList, pwd);
        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 返回
     */
    private void back() {
        LogToFile.d("====转账详情页====", "====返回主页====");
        mDevice.pressBack();
    }

    private void showLog(String msg) {
        LogToFile.showLog("兴业银行", msg);
    }

}